//
//  LeftPageMenuView.m
//  UcarTest
//
//  Created by 陈曦 on 16/1/13.
//  Copyright © 2016年 chenxi. All rights reserved.
//

#import "LeftPageMenuView.h"
#import "Define.h"
#import "Userinfo.h"
#import "AFNetworking.h"
#import "NetRequestGroup.h" 
#import "Access_token.h"
#import "MBProgressHUD.h"


#define kAccountViewSpace 50

@implementation LeftPageMenuView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        //菜单视图
        self.menuView = [[UIView alloc] initWithFrame:self.bounds];
        self.menuView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.menuView];

        //判断是否登录,决定加载哪个界面
        Userinfo *user = [Userinfo shareInstance];

        if (user.isLogin == NO) {
            
            [self NotLoggedIn];//加载未登录界面
            
        } else if (user.isLogin == YES){
            
            [self setupOfTheButton];//初始化各控件
            
            [self NetworkRequests];//网络解析
            
        }


    }
    return self;
}

#pragma mark 网络请求
- (void) NetworkRequests{
    //初始化进度框，置于当前的View当中
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self];
    [self addSubview:HUD];
    HUD.label.text = @"正在加载";
    [HUD showAnimated:YES];//HUD开始
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //申明返回的结果是json类型
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //申明请求的数据是json类型
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //如果报接受类型不一致请替换一致text/html或别的
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    //用户访问令牌
    Access_token *useAccess = [Access_token shareInstance];
    
    //上传的参数
    NSDictionary *para = @{
                           @"type" : @"1",
                           @"access_token" : useAccess.access_token
                           };
    //你的接口地址
    NSString *url = [NSString stringWithFormat:@"%@%@", kRealmName,kNetMemberInfo];
    //发送请求
    [manager POST:url parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic = responseObject;
        
        [HUD hideAnimated:YES];
        
        NSDictionary *info = dic[@"info"];
        
        self.is_pay = info[@"is_pay"];//是否设置过密码
        
        self.member_role_b = info[@"member_role_b"];//共享车主
        
        self.member_role_c = info[@"member_role_c"];//共享租客
        
        self.member_str = info[@"member_str"];//会员称呼
        
        self.amount_money = info[@"amount_money"];//总金额
        
        self.surplus_money = info[@"surplus_money"];//余额
        
        self.amount_pledge_money = info[@"amount_pledge_money"];//总押金
        
        self.pledge_money = info[@"pledge_money"];//押金
        
        self.credit_money = info[@"credit_money"];//信用额度
        
        self.freeze_pledge_money = info[@"freeze_pledge_money"];//冻结资金
        
        self.bank_card_count = info[@"bank_card_count"];//绑定银行卡数量
        
        //姓名标签创建
        self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake((kMenuViewWidth - kScreenWidthEight * 2) / 2, kScreenHeightTen * 1.5, kScreenWidthEight * 2, kScreenHeightTen / 2.5)];
        self.nameLabel.text = [NSString stringWithFormat:@"%@", self.member_str];
        self.nameLabel.textColor = wordColor;
        self.nameLabel.font = [UIFont systemFontOfSize:12];
        self.nameLabel.textAlignment = NSTextAlignmentCenter;
        [self.menuView addSubview:self.nameLabel];

        
        
        //判断登录界面状态
        if ([self.member_role_b isEqualToString:@"1"] && [self.member_role_c isEqualToString:@"1"]) {
            
            [self DownloadSuccessOne];//加载登录成功界面,车主租客都已经认证
            
        } else if ([self.member_role_b isEqualToString:@"1"] && [self.member_role_c isEqualToString:@"0"]) {
            
            [self DownloadSuccessTwo];//租客未认证
            
        } else if ([self.member_role_b isEqualToString:@"0"] && [self.member_role_c isEqualToString:@"1"]) {
            
            [self DownloadSuccessThree];//车主未认证
            
        } else if ([self.member_role_b isEqualToString:@"0"] && [self.member_role_c isEqualToString:@"0"]) {
            
            [self DownloadSuccessFour];//车主租客都未认证
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        [HUD hideAnimated:YES];
        
         
    }];
    
}
//---------------------------此处以上-----------------------------------------


#pragma mark 未登录界面
- (void) NotLoggedIn{
    
    //登陆按钮的创建
    self.loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loginBtn.frame = CGRectMake(kMenuViewWidth / 3, kMenuViewWidth / 4, kMenuViewWidth / 3, kMenuViewWidth / 10);
    self.loginBtn.layer.cornerRadius = 5;//圆角
    //    self.loginBtn.layer.borderWidth = 0;//边框线宽
    //    self.loginBtn.layer.masksToBounds = YES;//超出边界隐藏
    [self.loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    self.loginBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.loginBtn.backgroundColor = mainColor;
    [self.menuView addSubview:self.loginBtn];
    
    //注册按钮的创建
    self.registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.registerBtn.frame = CGRectMake(kMenuViewWidth / 3, kMenuViewWidth / 4 + kMenuViewWidth / 5, kMenuViewWidth / 3, kMenuViewWidth / 10);
    self.registerBtn.layer.cornerRadius = 5;//圆角
    [self.registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    self.registerBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.registerBtn setBackgroundColor:[UIColor orangeColor]];
    [self.menuView addSubview:self.registerBtn];
    
}

#pragma mark 初始化控件
- (void) setupOfTheButton {
    
    //头像视图创建
    self.headPhotoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.headPhotoBtn.frame = CGRectMake((kMenuViewWidth - kScreenWidthEight) / 2, kScreenHeightTen / 2, kScreenWidthEight * 3.74 / 3.07, kScreenWidthEight);
    [self.headPhotoBtn setImage:[UIImage imageNamed:@"iconfont-touxiang"] forState:UIControlStateNormal];
    [self.menuView addSubview:self.headPhotoBtn];
    
    
    //租客认证控件
    self.teantCertificationBtn = [DIYMenuButton buttonWithType:UIButtonTypeCustom];
    self.teantCertificationBtn.nameplateImageView.image = [UIImage imageNamed:@"icon-zuhu"];
    self.teantCertificationBtn.nameplateImageView.frame = CGRectMake(kScreenWidthTen / 2, kAccountViewSpace / 4, kAccountViewSpace / 2, kAccountViewSpace / 2);
    self.teantCertificationBtn.functionLabel.text = @"租客认证";
    
    //车主中心控件
    self.ownerCenterBtn = [DIYMenuButton buttonWithType:UIButtonTypeCustom];
    self.ownerCenterBtn.nameplateImageView.image = [UIImage imageNamed:@"icon-chezu"];
    self.ownerCenterBtn.nameplateImageView.frame = CGRectMake(kScreenWidthTen / 2, kAccountViewSpace / 4, kAccountViewSpace / 2, kAccountViewSpace / 2);
    self.ownerCenterBtn.functionLabel.text = @"车主中心";
    
    //租客订单控件
    self.teantIndentBtn = [DIYMenuButton buttonWithType:UIButtonTypeCustom];
    self.teantIndentBtn.nameplateImageView.image = [UIImage imageNamed:@"iconfont-dingdan"];
    self.teantIndentBtn.nameplateImageView.frame = CGRectMake(kScreenWidthTen / 2, kAccountViewSpace / 4, kAccountViewSpace / 2, kAccountViewSpace / 2);
    self.teantIndentBtn.functionLabel.text = @"租客订单";
    
    //车主订单控件
    self.ownerIndentBtn = [DIYMenuButton buttonWithType:UIButtonTypeCustom];
    self.ownerIndentBtn.nameplateImageView.image = [UIImage imageNamed:@"icon-car-indent"];
    self.ownerIndentBtn.nameplateImageView.frame = CGRectMake(kScreenWidthTen / 2, kAccountViewSpace / 4, kAccountViewSpace / 2 * 1.66 / 1.69, kAccountViewSpace / 2);
    self.ownerIndentBtn.functionLabel.text = @"车主订单";
    
}

#pragma mark 登录成功界面 全部控件都有
- (void) DownloadSuccessOne{
    
    //账户菜单
    UIView *accountView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeightTen * 2.5, kMenuViewWidth, kAccountViewSpace * 4)];
    [self.menuView addSubview:accountView];
    
    //租客认证控件
    self.teantCertificationBtn.frame = CGRectMake(0, 0, kMenuViewWidth, kAccountViewSpace);
    [accountView addSubview:self.teantCertificationBtn];
    
    UILabel *identificationLabel = [[UILabel alloc] initWithFrame:CGRectMake(kMenuViewWidth - 56, kAccountViewSpace / 4 + 6, 36, 12)];
    identificationLabel.backgroundColor = [UIColor whiteColor];
    identificationLabel.textColor = mainColor;
    identificationLabel.text = @"已认证";
    identificationLabel.font = [UIFont systemFontOfSize:12];
    [self.teantCertificationBtn addSubview:identificationLabel];

    
    //车主中心控件
    self.ownerCenterBtn.frame = CGRectMake(0, kAccountViewSpace, kMenuViewWidth, kAccountViewSpace);
    [accountView addSubview:self.ownerCenterBtn];
    
    UILabel *identificationLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(kMenuViewWidth - 56, kAccountViewSpace / 4 + 6, 36, 12)];
    identificationLabel1.backgroundColor = [UIColor whiteColor];
    identificationLabel1.textColor = mainColor;
    identificationLabel1.text = @"已认证";
    identificationLabel1.font = [UIFont systemFontOfSize:12];
    [self.ownerCenterBtn addSubview:identificationLabel1];

    
    //租客订单控件
    self.teantIndentBtn.frame = CGRectMake(0, kAccountViewSpace * 2, kMenuViewWidth, kAccountViewSpace);
    [accountView addSubview:self.teantIndentBtn];
    
    //车主订单控件
    self.ownerIndentBtn.frame = CGRectMake(0, kAccountViewSpace * 3, kMenuViewWidth, kAccountViewSpace);
    [accountView addSubview:self.ownerIndentBtn];
    
    
    //分割线1--------------------------------------------------------------
    UILabel *lineLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kMenuViewWidth, 0.5)];
    lineLabel1.backgroundColor = lineColor;
    [accountView addSubview:lineLabel1];
    
    UILabel *lineLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidthTen / 2, kAccountViewSpace, kMenuViewWidth - kScreenWidthTen / 2, 0.5)];
    lineLabel2.backgroundColor = lineColor;
    [accountView addSubview:lineLabel2];
    
    UILabel *lineLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidthTen / 2, kAccountViewSpace * 2, kMenuViewWidth - kScreenWidthTen / 2, 0.5)];
    lineLabel3.backgroundColor = lineColor;
    [accountView addSubview:lineLabel3];
    
    UILabel *lineLabel4 = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidthTen / 2, kAccountViewSpace * 3, kMenuViewWidth - kScreenWidthTen / 2, 0.5)];
    lineLabel4.backgroundColor = lineColor;
    [accountView addSubview:lineLabel4];
    
    UILabel *lineLabel5 = [[UILabel alloc] initWithFrame:CGRectMake(0, kAccountViewSpace * 4, kMenuViewWidth, 0.5)];
    lineLabel5.backgroundColor = lineColor;
    [accountView addSubview:lineLabel5];
    //--------------------------------------------------------------------
}

#pragma mark 登录成功界面 租客未认证
- (void) DownloadSuccessTwo {
    
    //账户菜单
    UIView *accountView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeightTen * 2.5, kMenuViewWidth, kAccountViewSpace * 3)];
    [self.menuView addSubview:accountView];
    
    //租客认证控件
    self.teantCertificationBtn.frame = CGRectMake(0, 0, kMenuViewWidth, kAccountViewSpace);
    [accountView addSubview:self.teantCertificationBtn];
    
    UILabel *identificationLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(kMenuViewWidth - 56, kAccountViewSpace / 4 + 6, 36, 12)];
    identificationLabel1.backgroundColor = [UIColor whiteColor];
    identificationLabel1.textColor = [UIColor orangeColor];
    identificationLabel1.text = @"未认证";
    identificationLabel1.font = [UIFont systemFontOfSize:12];
    [self.teantCertificationBtn addSubview:identificationLabel1];

    
    //车主中心控件
    self.ownerCenterBtn.frame = CGRectMake(0, kAccountViewSpace, kMenuViewWidth, kAccountViewSpace);
    [accountView addSubview:self.ownerCenterBtn];
    
    UILabel *identificationLabel = [[UILabel alloc] initWithFrame:CGRectMake(kMenuViewWidth - 56, kAccountViewSpace / 4 + 6, 36, 12)];
    identificationLabel.backgroundColor = [UIColor whiteColor];
    identificationLabel.textColor = mainColor;
    identificationLabel.text = @"已认证";
    identificationLabel.font = [UIFont systemFontOfSize:12];
    [self.ownerCenterBtn addSubview:identificationLabel];

    
    //车主订单控件
    self.ownerIndentBtn.frame = CGRectMake(0, kAccountViewSpace * 2, kMenuViewWidth, kAccountViewSpace);
    [accountView addSubview:self.ownerIndentBtn];
    
    
    //分割线1--------------------------------------------------------------
    UILabel *lineLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kMenuViewWidth, 0.5)];
    lineLabel1.backgroundColor = lineColor;
    [accountView addSubview:lineLabel1];
    
    UILabel *lineLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidthTen / 2, kAccountViewSpace, kMenuViewWidth - kScreenWidthTen / 2, 0.5)];
    lineLabel2.backgroundColor = lineColor;
    [accountView addSubview:lineLabel2];
    
    UILabel *lineLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidthTen / 2, kAccountViewSpace * 2, kMenuViewWidth - kScreenWidthTen / 2, 0.5)];
    lineLabel3.backgroundColor = lineColor;
    [accountView addSubview:lineLabel3];
    
    UILabel *lineLabel4 = [[UILabel alloc] initWithFrame:CGRectMake(0, kAccountViewSpace * 3, kMenuViewWidth, 0.5)];
    lineLabel4.backgroundColor = lineColor;
    [accountView addSubview:lineLabel4];
    //--------------------------------------------------------------------
}

#pragma mark 登录成功界面 车主未认证
- (void) DownloadSuccessThree {
    
    //账户菜单
    UIView *accountView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeightTen * 2.5, kMenuViewWidth, kAccountViewSpace * 3)];
    [self.menuView addSubview:accountView];
    
    //租客认证控件
    self.teantCertificationBtn.frame = CGRectMake(0, 0, kMenuViewWidth, kAccountViewSpace);
    [accountView addSubview:self.teantCertificationBtn];
    
    //车主中心控件
    self.ownerCenterBtn.frame = CGRectMake(0, kAccountViewSpace, kMenuViewWidth, kAccountViewSpace);
    self.ownerCenterBtn.userInteractionEnabled = NO;
    [accountView addSubview:self.ownerCenterBtn];
    
//    UIView *ownerCenterView = [[UIView alloc] initWithFrame:CGRectMake(0, kAccountViewSpace, kMenuViewWidth, kAccountViewSpace)];
//    ownerCenterView.backgroundColor = [UIColor clearColor];
//    [accountView addSubview:ownerCenterView];
    
    UILabel *identificationLabel = [[UILabel alloc] initWithFrame:CGRectMake(kMenuViewWidth - 56, kAccountViewSpace / 4 + 6, 36, 12)];
    identificationLabel.backgroundColor = [UIColor whiteColor];
    identificationLabel.textColor = mainColor;
    identificationLabel.text = @"已认证";
    identificationLabel.font = [UIFont systemFontOfSize:12];
    [self.teantCertificationBtn addSubview:identificationLabel];
    
    UILabel *identificationLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(kMenuViewWidth - 56, kAccountViewSpace / 4 + 6, 36, 12)];
    identificationLabel1.backgroundColor = [UIColor whiteColor];
    identificationLabel1.textColor = [UIColor orangeColor];
    identificationLabel1.text = @"未认证";
    identificationLabel1.font = [UIFont systemFontOfSize:12];
    [self.ownerCenterBtn addSubview:identificationLabel1];

    
    //租客订单控件
    self.teantIndentBtn.frame = CGRectMake(0, kAccountViewSpace * 2, kMenuViewWidth, kAccountViewSpace);
    [accountView addSubview:self.teantIndentBtn];
    
    
    //分割线1--------------------------------------------------------------
    UILabel *lineLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kMenuViewWidth, 0.5)];
    lineLabel1.backgroundColor = lineColor;
    [accountView addSubview:lineLabel1];
    
    UILabel *lineLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidthTen / 2, kAccountViewSpace, kMenuViewWidth - kScreenWidthTen / 2, 0.5)];
    lineLabel2.backgroundColor = lineColor;
    [accountView addSubview:lineLabel2];
    
    UILabel *lineLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidthTen / 2, kAccountViewSpace * 2, kMenuViewWidth - kScreenWidthTen / 2, 0.5)];
    lineLabel3.backgroundColor = lineColor;
    [accountView addSubview:lineLabel3];
    
    UILabel *lineLabel4 = [[UILabel alloc] initWithFrame:CGRectMake(0, kAccountViewSpace * 3, kMenuViewWidth, 0.5)];
    lineLabel4.backgroundColor = lineColor;
    [accountView addSubview:lineLabel4];
    //--------------------------------------------------------------------
}

#pragma mark 登录成功界面 车主和租客都未认证
- (void) DownloadSuccessFour {
    
    //账户菜单
    UIView *accountView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeightTen * 2.5, kMenuViewWidth, kAccountViewSpace * 2)];
    [self.menuView addSubview:accountView];
    
    //租客认证控件
    self.teantCertificationBtn.frame = CGRectMake(0, 0, kMenuViewWidth, kAccountViewSpace);
    [accountView addSubview:self.teantCertificationBtn];
    
    //车主中心控件
    self.ownerCenterBtn.frame = CGRectMake(0, kAccountViewSpace, kMenuViewWidth, kAccountViewSpace);
    self.ownerCenterBtn.userInteractionEnabled = NO;
    [accountView addSubview:self.ownerCenterBtn];
    
    UILabel *identificationLabel = [[UILabel alloc] initWithFrame:CGRectMake(kMenuViewWidth - 56, kAccountViewSpace / 4 + 6, 36, 12)];
    identificationLabel.backgroundColor = [UIColor whiteColor];
    identificationLabel.textColor = [UIColor orangeColor];
    identificationLabel.text = @"未认证";
    identificationLabel.font = [UIFont systemFontOfSize:12];
    [self.teantCertificationBtn addSubview:identificationLabel];
    
    UILabel *identificationLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(kMenuViewWidth - 56, kAccountViewSpace / 4 + 6, 36, 12)];
    identificationLabel1.backgroundColor = [UIColor whiteColor];
    identificationLabel1.textColor = [UIColor orangeColor];
    identificationLabel1.text = @"未认证";
    identificationLabel1.font = [UIFont systemFontOfSize:12];
    [self.ownerCenterBtn addSubview:identificationLabel1];

    
    //分割线1--------------------------------------------------------------
    UILabel *lineLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kMenuViewWidth, 0.5)];
    lineLabel1.backgroundColor = lineColor;
    [accountView addSubview:lineLabel1];
    
    UILabel *lineLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidthTen / 2, kAccountViewSpace, kMenuViewWidth - kScreenWidthTen / 2, 0.5)];
    lineLabel2.backgroundColor = lineColor;
    [accountView addSubview:lineLabel2];
    
    UILabel *lineLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(0, kAccountViewSpace * 2, kMenuViewWidth, 0.5)];
    lineLabel3.backgroundColor = lineColor;
    [accountView addSubview:lineLabel3];
    //--------------------------------------------------------------------
}


@end
